﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Web.UI.WebControls;
//
using System.Data;
using PerceptiveMCAPI;
using PerceptiveMCAPI.Types;
//

namespace PerceptiveMCAPI_Test
{
   public partial class show_listMemberInfo : System.Web.UI.UserControl
   {
      public void Display( listMemberInfoOutput output )
      {
         this.Visible = true;
         // ------------------------------------------------
         // manual format to get merge variables to bind
         // ------------------------------------------------
         DataTable tbl = new DataTable( "listMemberInfoResult" );
         tbl.Columns.Add( "id" ); tbl.Columns.Add( "Email_Address" ); tbl.Columns.Add( "Email_Type" );
         tbl.Columns.Add( "Status" ); tbl.Columns.Add( "ip_opt" ); tbl.Columns.Add( "ip_signup" );
         tbl.Columns.Add( "member_rating" ); tbl.Columns.Add( "campaign_id" ); tbl.Columns.Add( "Timestamp" );
         tbl.Columns.Add( "info_changed" ); tbl.Columns.Add( "web_id" );
         tbl.Columns.Add( "MergeVars" ); tbl.Columns.Add( "Groupings" ); tbl.Columns.Add( "Lists" );

         DataRow row = tbl.NewRow();
         row["id"] = output.result.id;
         row["Email_Address"] = output.result.email;
         row["Email_Type"] = output.result.email_type.ToString();
         row["Status"] = output.result.status.ToString();
         row["ip_opt"] = output.result.ip_opt;
         row["ip_signup"] = output.result.ip_signup;
         row["member_rating"] = output.result.member_rating;
         row["campaign_id"] = output.result.campaign_id;
         row["TimeStamp"] = ( output.result.timestamp == DateTime.MinValue ) ? "" : output.result.timestamp.ToString();
         row["info_changed"] = ( output.result.info_changed == DateTime.MinValue ) ? "" : output.result.info_changed.ToString();
         row["web_id"] = output.result.web_id;
         //
         row["MergeVars"] = FormatMergeVars( output.result.merges );
         row["Groupings"] = FormatGroupings( output.result.groupings );
         row["Lists"] = FormatLists( output.result.lists );
         //
         tbl.Rows.Add( row );
         // ------------------------------------------------
         dvListMemberInfo.HeaderText = string.Format( "Member Information for list: {0}", output.inputParms.id );
         dvListMemberInfo.DataSource = tbl;
         dvListMemberInfo.DataBind();
      }
      // 
      private string FormatMergeVars( Dictionary<string, object> mergevars )
      {
         if ( mergevars.Count > 0 )
         {
            SortedDictionary<string, string> sDict = new SortedDictionary<string, string>();
            foreach ( KeyValuePair<string, object> kv in mergevars )
            {
               if ( kv.Value.GetType().Name.ToLower().Contains( "dictionary" ) )
               {
                  Dictionary<string, object> dict = kv.Value as Dictionary<string, object>;
                  sDict.Add( kv.Key, ApiHelper.DictionaryToString( dict ) );
               }
               else
                  sDict.Add( kv.Key, kv.Value.ToString() );
            }
            return ApiHelper.SortedDictionaryToStringHtmlStack( sDict );
         }
         else
         {
            return "** No MergeVars Found";
         }

      }
      // -------------------------------------
      private string FormatGroupings( List<interestGroupings> groupings )
      {
         if ( groupings.Count > 0 )
         {
            int ix = 0;
            StringBuilder sb = new StringBuilder();
            foreach ( var grouping in groupings )
            {
               string groups = "[ No group selected ]";
               ix++;
               if ( grouping.groups.Count > 0 )
                  groups = ApiHelper.MakeStringFromList( grouping.groups );
               //
               sb.AppendFormat( "id=>{0}, name=>{1}, <br />groups=>{2}",
                  grouping.id, grouping.name, groups );
               //
               if ( ix != groupings.Count ) sb.Append( "<hr />" );
            }
            //
            return sb.ToString();
         }
         else
         {
            return "** No Groupings Found";
         }
      }
      // -------------------------------------
      private string FormatLists( Dictionary<string, EnumValues.listMembers_status> lists )
      {
         if ( lists.Count > 0 )
         {
            SortedDictionary<string, string> sDict = new SortedDictionary<string, string>();
            foreach ( KeyValuePair<string, EnumValues.listMembers_status> kv in lists )
            {
               sDict.Add( kv.Key, kv.Value.ToString() );
            }
            return ApiHelper.SortedDictionaryToStringHtmlStack( sDict );
         }
         else
         {
            return "** No Lists Found";
         }
      }
      // -------------------------------------
   }
}